import request from '@/utils/request'

// ==================== 图书管理 ====================

// 获取图书列表
export function getBookList(params) {
  return request({
    url: '/v1/books',
    method: 'get',
    params
  })
}

// 获取图书详情
export function getBookDetail(id) {
  return request({
    url: `/v1/books/${id}`,
    method: 'get'
  })
}

// 搜索图书
export function searchBooks(keyword, params) {
  return request({
    url: '/v1/books/search',
    method: 'get',
    params: { keyword, ...params }
  })
}

// 获取热门图书
export function getHotBooks(limit = 10) {
  return request({
    url: '/v1/books/hot',
    method: 'get',
    params: { limit }
  })
}

// 获取推荐图书
export function getRecommendBooks(limit = 10) {
  return request({
    url: '/v1/books/recommend',
    method: 'get',
    params: { limit }
  })
}

// 按分类获取图书
export function getBooksByCategory(categoryId, params) {
  return request({
    url: `/v1/books/category/${categoryId}`,
    method: 'get',
    params
  })
}

// 收藏图书
export function favoriteBook(bookId) {
  return request({
    url: `/v1/books/${bookId}/favorite`,
    method: 'post'
  })
}

// 取消收藏
export function unfavoriteBook(bookId) {
  return request({
    url: `/v1/books/${bookId}/unfavorite`,
    method: 'post'
  })
}

// 获取收藏图书
export function getFavoriteBooks(params) {
  return request({
    url: '/v1/books/favorites',
    method: 'get',
    params
  })
}

// 获取相关图书
export function getRelatedBooks(bookId, limit = 6) {
  return request({
    url: `/v1/books/${bookId}/related`,
    method: 'get',
    params: { limit }
  })
}

// 获取图书评论
export function getBookComments(bookId, params) {
  return request({
    url: `/v1/books/${bookId}/comments`,
    method: 'get',
    params
  })
}

// 添加图书评论
export function addBookComment(data) {
  return request({
    url: '/v1/book-comments',
    method: 'post',
    data
  })
}

// ==================== 借阅管理 ====================

// 借阅图书
export function borrowBook(bookId) {
  return request({
    url: '/v1/borrows/borrow',
    method: 'post',
    data: { bookId }
  })
}

// 归还图书
export function returnBook(borrowId) {
  return request({
    url: '/v1/borrows/return',
    method: 'post',
    data: { borrowId }
  })
}

// 续借图书
export function renewBook(borrowId) {
  return request({
    url: '/v1/borrows/renew',
    method: 'post',
    data: { borrowId }
  })
}

// 获取我的借阅记录
export function getMyBorrowingRecords(params) {
  return request({
    url: '/v1/borrows/my',
    method: 'get',
    params
  })
}

// 获取借阅统计信息
export function getBorrowingStatistics() {
  return request({
    url: '/v1/borrows/statistics',
    method: 'get'
  })
}

// 获取当前借阅
export function getCurrentBorrows() {
  return request({
    url: '/v1/borrows/current',
    method: 'get'
  })
}

// 获取借阅历史
export function getBorrowHistory(params) {
  return request({
    url: '/v1/borrows/history',
    method: 'get',
    params
  })
}

// 检查借阅状态
export function checkBorrowStatus(bookId) {
  return request({
    url: `/v1/borrows/status/${bookId}`,
    method: 'get'
  })
}

// ==================== 预约管理 ====================

// 预约图书
export function reserveBook(bookId) {
  return request({
    url: '/v1/reservations',
    method: 'post',
    data: { bookId }
  })
}

// 取消预约
export function cancelReservation(reservationId) {
  return request({
    url: `/v1/reservations/${reservationId}/cancel`,
    method: 'post'
  })
}

// 获取我的预约
export function getMyReservations(params) {
  return request({
    url: '/v1/reservations/my',
    method: 'get',
    params
  })
}

// ==================== 图书分类 ====================

// 获取图书分类
export function getBookCategories() {
  return request({
    url: '/v1/categories',
    method: 'get'
  })
}

// 获取分类树
export function getBookCategoryTree() {
  return request({
    url: '/v1/categories/tree/all',
    method: 'get'
  })
}

// ==================== 图书评价 ====================

// 获取图书评价
export function getBookReviews(bookId, params) {
  return request({
    url: `/v1/book-reviews/book/${bookId}`,
    method: 'get',
    params
  })
}

// 添加评价
export function addBookReview(data) {
  return request({
    url: '/v1/book-reviews',
    method: 'post',
    data
  })
}

// 点赞评价
export function likeBookReview(reviewId) {
  return request({
    url: `/v1/book-reviews/${reviewId}/like`,
    method: 'post'
  })
}

// ==================== 阅读记录 ====================

// 获取阅读记录
export function getReadingRecords(params) {
  return request({
    url: '/v1/reading-records',
    method: 'get',
    params
  })
}

// 添加阅读记录
export function addReadingRecord(data) {
  return request({
    url: '/v1/reading-records',
    method: 'post',
    data
  })
}

// 更新阅读进度
export function updateReadingProgress(recordId, progress) {
  return request({
    url: `/v1/reading-records/${recordId}/progress`,
    method: 'put',
    data: { progress }
  })
}

// 获取阅读统计
export function getReadingStatistics() {
  return request({
    url: '/v1/reading-records/statistics',
    method: 'get'
  })
}

// ==================== 分类相关 ====================

// 获取图书分类列表
export function getBookCategories() {
  return request({
    url: '/v1/categories',
    method: 'get'
  })
}

// 获取图书分类详情
export function getBookCategory(categoryId) {
  return request({
    url: `/v1/categories/${categoryId}`,
    method: 'get'
  })
}

// 根据分类获取图书
export function getBooksByCategory(categoryId, params) {
  return request({
    url: `/v1/categories/${categoryId}/books`,
    method: 'get',
    params
  })
}
